package com.example.zwh.computershop.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zwh.computershop.Home_Activity;
import com.example.zwh.computershop.Myapp;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.androidUtil.ActivityCollector;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.sqlModel.Cart;
import com.example.zwh.computershop.sqlModel.Order;
import com.example.zwh.computershop.sqlModel.User;

import java.util.ArrayList;

public class Login extends AppCompatActivity implements View.OnClickListener {

    Intent intent;
    Toast toast;
    TextView register_skip;
    Button btn_login;
    EditText login_em;
    EditText login_pwd;


    int state = -1;
    String psw;
    String em;
    User user;
    Myapp myapp;

    //private int auto = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);
        ActivityCollector.addActivity(this);
        initView();
        //autoLogin();
        initEvent();
    }
//存了信息就提取自动登录
//    public void autoLogin() {
//        File file = new File("content.WebInfo");
//        if(!file.exists()) return;
//        AutoUser auser = null;
//        try {
//            FileInputStream fis = new FileInputStream(file);
//            ObjectInputStream ois = new ObjectInputStream(fis);
//            Object obj = null;
//            try {
//                auser = (AutoUser) ois.readObject();
//                ois.close();
//                fis.close();
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        auto = 0;
//        em = auser.getEmail();
//        psw = auser.getPassword();
//        Login(user);
//    }

    public void initView() {
        //获取个存登录的单列类
        myapp = (Myapp) getApplication();
        register_skip = (TextView) findViewById(R.id.register_skip);
        btn_login = (Button) findViewById(R.id.btn_login);
        login_em = (EditText) findViewById(R.id.em);
        login_pwd = (EditText) findViewById(R.id.pwd);
        user = new User();
        login_em.setText("zwhzsjwx@163.com");
        login_pwd.setText("z");
    }

    public void initEvent() {
        register_skip.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())  {
            case R.id.btn_login :

                Login(user);
                //user.printInfo();
                System.out.println("***"+state);

                if (state == 2) {
                    //user.printInfo();
                    toast.makeText(Login.this, "登录成功", Toast.LENGTH_SHORT).show();
                    //登录成功
                    myapp.setUser(user);
                    myapp.setLoState(1);
                    intent = new Intent(Login.this, Home_Activity.class);
                    startActivity(intent);
                    finish();
                }else if (state == -1) {
                    toast.makeText(Login.this, "不能为空", Toast.LENGTH_SHORT).show();
                }else if (state == 1) {
                    toast.makeText(Login.this, "账户不存在", Toast.LENGTH_SHORT).show();
                }else if (state == 3) {
                    toast.makeText(Login.this, "密码错误", Toast.LENGTH_SHORT).show();
                }else if (state == 5) {
                    toast.makeText(Login.this, "网络错误", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.register_skip :
                // toast.makeText(Login.this, "注册", Toast.LENGTH_SHORT).show();
                intent = new Intent(Login.this, RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }

    public void Login(User user) {
        //if (auto == 1) {

            em = login_em.getText().toString();
            psw = login_pwd.getText().toString();
            if (em.length() == 0 || em == null || em.compareTo("") == 0) {
                System.out.println("邮箱为空");
                toast.makeText(Login.this, "邮箱为空", Toast.LENGTH_SHORT).show();
                return;
            }
            if (psw.length() == 0 || psw == null || em.compareTo("") == 0){
                System.out.println("密码为空");
                toast.makeText(Login.this, "密码为空", Toast.LENGTH_SHORT).show();
                return;
            }
            Thread t = new Thread(new takeThread());
            t.start();
            try {
                t.join();
            }catch (InterruptedException ie) {
                ie.printStackTrace();
            }
//        }else {
//            Thread t = new Thread(new takeThread());
//            t.start();
//            try {
//                t.join();
//            }catch (InterruptedException ie) {
//                ie.printStackTrace();
//            }
//            myapp.setUser(user);
//            myapp.setLoState(1);
//            intent = new Intent(Login.this, Home_Activity.class);
//            startActivity(intent);
//            finish();
//        }
    }

    class takeThread extends Thread {
        @Override
        public void run() {
            state = handleData.handleLogin(em, psw, user);
            if (state == 2) {
                ArrayList<Cart> cat = new ArrayList<>();

                int state2 = handleData.getCart(cat, user.getId());
                if (state2 == 3) {
                    myapp.setCart(cat);
                }

                ArrayList<Order> ord = new ArrayList<>();
                state2 = handleData.getOrd(ord, user.getId());
                if (state2 == 3) {
                    myapp.setOra(ord);
                }
            }
            user.printInfo();
        }
    }
//保存用户信息
//    public static void saveUser(AutoUser autoUser) {
//        try {
//            File file = new File("content.WebInfo");
//            if(!file.exists()) {
//                try {
//                    file.createNewFile();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            FileOutputStream fos = new FileOutputStream(file);
//            ObjectOutputStream oos = new ObjectOutputStream(fos);
//            oos.writeObject(autoUser);
//            oos.close();
//            fos.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
